//
//  HomePageViewController.m
//  test1
//
//  Created by 衣尚品1 on 15/4/29.
//  Copyright (c) 2015年 衣尚品1. All rights reserved.
//

#import "HomePageViewController.h"

@interface HomePageViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;
@property (weak, nonatomic) IBOutlet UIButton *MyClassButton;
@property (weak, nonatomic) IBOutlet UIButton *MyTeacherButton;
@property (weak, nonatomic) IBOutlet UIButton *AllSubjectButton;
@property (weak, nonatomic) IBOutlet UIButton *MatchTeacherButton;

@end

@implementation HomePageViewController{
    //当前页数
    int pageInt;

    //下一页
    int nextPage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleScrollView.delegate = self;
    
    //设置四个按钮的圆角
    self.MyClassButton.layer.cornerRadius = 10;
    self.MyTeacherButton.layer.cornerRadius = 10;
    self.AllSubjectButton.layer.cornerRadius = 10;
    self.MatchTeacherButton.layer.cornerRadius = 10;
    
    [self setupPageControl];
    
    [self setUpScrollView];
    
    
}

#pragma mark - 把图片放进scrollview，设置scrollview
- (void)setUpScrollView{
    //添加scrollview的图片
    NSArray *arr = [[NSArray alloc] initWithObjects:@"teacher1.png",@"teacher2.png",@"teacher3.png", nil];
    for (int i = 0; i < arr.count; i++) {
        UIImageView *imageview = [[UIImageView alloc] init];
        imageview.image = [UIImage imageNamed:arr[i]];
        
        imageview.frame = CGRectMake(self.titleScrollView.frame.size.width * i, 0, self.titleScrollView.frame.size.width, self.titleScrollView.frame.size.height);
        
        [self.titleScrollView addSubview:imageview];
    }
    
    self.titleScrollView.contentSize = CGSizeMake(self.titleScrollView.frame.size.width * 3, self.titleScrollView.frame.size.height);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.pagingEnabled = YES;
    self.titleScrollView.bounces = NO;
    
    //定时滑动
    NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    [myTimer setFireDate:[NSDate distantPast]];
}

#pragma mark - 获取强制分页的页码
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //1.取出水平方向的滚动距离
    CGFloat offsetX=scrollView.contentOffset.x;
    
    //2.求出页码
    double pageDouble=offsetX / scrollView.frame.size.width;
    //四舍五入
    pageInt=(int)(pageDouble + 0.5);
    self.pageControl.currentPage=pageInt;
    
}

#pragma mark - scrollview的自动滚动
-(void)scrollTimer{
    
    [self.titleScrollView scrollRectToVisible:CGRectMake(self.titleScrollView.frame.size.width * nextPage, 0, self.titleScrollView.frame.size.width, self.titleScrollView.frame.size.height) animated:YES];
    
    nextPage++;
    if (pageInt == 2) {
        nextPage=0;
    }
}

#pragma mark - 添加页码的点
-(void)setupPageControl{
    //1.添加
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = 3;
    
    CGFloat centerX = 320 * 0.5;
    CGFloat centerY = 210;
    pageControl.center = CGPointMake(centerX, centerY);
    pageControl.bounds = CGRectMake(0, 0, 100, 300);//设置pageview 的宽高
    
    pageControl.userInteractionEnabled = NO;//撤销page的点击事件
    [self.view addSubview:pageControl];

    self.pageControl = pageControl;
    //2.设置圆点颜色
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:253/255.0 green:98/255.0 blue:42/255.0 alpha:1.0];
    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1.0];
    
    
}
- (IBAction)clickMyClass:(id)sender {
}
- (IBAction)clickMyTeacher:(id)sender {
}
- (IBAction)clickAllSubject:(id)sender {
}
- (IBAction)clickMatchTeacher:(id)sender {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)Change_Identity:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否切换到老师端" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    [alert show];
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSLog(@"否");
        
    }
    else
    {
        NSLog(@"是");
        
        UITabBarController *tabbar = [self.storyboard instantiateViewControllerWithIdentifier:@"laoshi"];
        [self presentViewController:tabbar animated:YES completion:nil];
    }
    
}

@end
